package com.sofwin.controller;/**
 * @PackageName:com.sofwin.controller
 * @author:andyLiu
 * @DateTime:2020/12/24 10:40
 * @Description:[TODO]输入当前模块作用
 **/

import com.github.pagehelper.PageInfo;
import com.sofwin.dto.UserDto;
import com.sofwin.pojo.SOrganization;
import com.sofwin.pojo.SRole;
import com.sofwin.pojo.SUser;
import com.sofwin.service.OrgService;
import com.sofwin.service.RoleService;
import com.sofwin.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: UserController
 * @Description: TODO
 * @Author: andyliu
 * @Date: 2020/12/24 10:40
 * @Version: 1.0
 **/
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private OrgService orgService;
    @Autowired
    private RoleService roleService;
    @RequestMapping("/data")
    public String data(SUser user, @RequestParam(required = true,defaultValue = "1") Integer pageNumber, Model model){
        // 调用业务层查询数据
       PageInfo<UserDto> page =  userService.queryUserForPage(user,pageNumber);
       model.addAttribute("page",page);
       model.addAttribute("user",user);
       return "user/data";
    }

    @GetMapping("delete")
    @ResponseBody
    public Map delete(SUser user){
        // 删除成功
        Map map = new HashMap();
        map.put("status",false);
        boolean status = userService.deleteUserById(user.getId());
        map.put("status",status);
        return map;
    }
    @RequestMapping("/deletes")
    @ResponseBody
    public Map deletes(Integer[] ids){
        Map map = new HashMap();
        boolean status = userService.deleteUsers(ids);
        map.put("status",status);
        return map;
    }
    @RequestMapping("/add")
    public String toAdd(SUser user,Model model){
        if(user.getId()!=null){
            // 编辑
            user = userService.selectUserById(user.getId());
            model.addAttribute("user",user);
        }
        // 查询所有部门
        List<SOrganization> orgs = orgService.queryOrgs();
        model.addAttribute("orgs",orgs);

        // 查询所有角色
        List<SRole> roles = roleService.queryRoles();
        model.addAttribute("roles",roles);
        return "user/add";
    }
    @RequestMapping("/save")
    @ResponseBody
    public Map saveUser(SUser user){
        Map map = new HashMap();
        boolean status = userService.saveUser(user);
        map.put("status",status);
        return map;
    }
}
